import pickle
import base64
import json


class CookieSecret(object):
    # 加密
    @classmethod
    def dumps(cls, data):

        # 1.将数据装换成bytes 将data--pickle--bytes
        data_bytes = pickle.dumps(data)
        # 2.将bytes使用base64序列化加密  bytes--base64--bytes
        base64_bytes = base64.b64encode(data_bytes)
        # 3.将加密完毕的bytes以字符串类型输出 bytes--decode--str
        base64_str = base64_bytes.decode()
        return base64_str
    # 解密--解码
    @classmethod
    def loads(cls, data):
        # 1.将数据解密转成bytes data-->base64--bytes
        base64_bytes = base64.b64decode(data)
        # 2.将bytes转回原来的python类型 bytes-->pickle-->原始数据类型
        pickle_data = pickle.loads(base64_bytes)
        return pickle_data
# 解密－－解码
# json 作用：pythondict　list --json_str互转
# data_dict = {
#   1:{
#       "count":2,
#       "selected":True,
#       }
# }
## dict--json_str互转
# json_str = json.dumps(data_dict)
#
## json_str ---dict
# json_dict = json.loads(json_str)

# pickle 作用： 将python对象－－－＞bytes--->还是原始数据类型
# pickle_bytes = pickle.dumps(data_dict)
#
# pickle_dict = pickle.loads(pickle_bytes)

# base64 编码解码
# base64 encode = base64.b64encode(pickle_bytes)
#
# base64 decode = base64.b64decode(base64_encode)
#
# print(base64_decode)
# print(pickle.loads(base64_decode))
